﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EarringsRecommendationWeb
{
    public partial class Compare : CheckUserSession
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                DataTable dtCompare = this.GetCompareEarrings();
                ClientScript.RegisterArrayDeclaration("EarringsCompare", JsonConvert.SerializeObject(dtCompare));
            }
        }

        private DataTable GetCompareEarrings()
        {
            string email = string.Empty;
            if (Session["member"] != null)
            {
                Member mem = Session["member"] as Member;
                email = mem.Email;
            }
            SqlParameter[] sqlParams = new SqlParameter[] 
            {
                new SqlParameter("@Email", email),
            };
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("SELECT DISTINCT er.Er_Code, er.Er_Name");
            sqlStr.Append(" , (SELECT Config_NameTh  FROM EarringsConfig erConf WHERE  erConf.Config_Id = er.Er_Style)AS [Er_Styles_Name]");
            sqlStr.Append(" , (SELECT Config_NameTh  FROM EarringsConfig erConf WHERE  erConf.Config_Id = er.Er_Shape)AS [Er_Shape_Name]");
            sqlStr.Append(" , (SELECT Config_NameTh  FROM EarringsConfig erConf WHERE  erConf.Config_Id = er.Er_Color)AS [Er_Color_Name]");
            sqlStr.Append(" ,(SELECT Config_NameTh  FROM EarringsConfig erConf WHERE  erConf.Config_Id = er.Er_EarBack)AS [Er_Ear_Back]");
            sqlStr.Append(" , er.Er_Gemstone, er.Er_Price, er.Er_Detail, com.CompareImg");
            sqlStr.Append(" FROM Earrings er");
            sqlStr.Append(" INNER JOIN EarringsCompare com");
            sqlStr.Append(" ON er.Er_Code = com.Er_Code");
            sqlStr.Append(" WHERE com.Email = @Email");

            DataTable dtResult = new DataTable();
            dtResult = SqlClientUtility.ExecuteDataTable(CommandType.Text, sqlStr.ToString(), sqlParams);

            return dtResult;
        }
    }
}